home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / Filezilla Server / FileZilla_Server-0_9_41.exe / source / interface / misc / SystemTray.cpp < prev    next >
C/C++ Source or Header  |  2011-11-06  |  33KB  |  1,130 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. // SystemTray.cpp : implementation file
  3. //
  4. // MFC VERSION
  5. //
  6. // This is a conglomeration of ideas from the MSJ "Webster" application,
  7. // sniffing round the online docs, and from other implementations such
  8. // as PJ Naughter's "CTrayNotifyIcon" (http://indigo.ie/~pjn/ntray.html)
  9. // especially the "CSystemTray::OnTrayNotification" member function.
  10. // Joerg Koenig suggested the icon animation stuff
  11. //
  12. // This class is a light wrapper around the windows system tray stuff. It
  13. // adds an icon to the system tray with the specified ToolTip text and 
  14. // callback notification value, which is sent back to the Parent window.
  15. //
  16. // The tray icon can be instantiated using either the constructor or by
  17. // declaring the object and creating (and displaying) it later on in the
  18. // program. eg.
  19. //
  20. //        CSystemTray m_SystemTray;    // Member variable of some class
  21. //        
  22. //        ... 
  23. //        // in some member function maybe...
  24. //        m_SystemTray.Create(pParentWnd, WM_MY_NOTIFY, "Click here", 
  25. //                          hIcon, nSystemTrayID);
  26. //
  27. // Written by Chris Maunder (cmaunder@mail.com)
  28. // Copyright (c) 1998.
  29. //
  30. // Updated: 25 Jul 1998 - Added icon animation, and derived class
  31. //                        from CWnd in order to handle messages. (CJM)
  32. //                        (icon animation suggested by Joerg Koenig.
  33. //                        Added API to set default menu item. Code provided
  34. //                        by Enrico Lelina.
  35. //
  36. // Updated: 6 June 1999 - SetIcon can now load non-standard sized icons (Chip Calvert)
  37. //                        Added "bHidden" parameter when creating icon
  38. //                        (Thanks to Michael Gombar for these suggestions)
  39. //                        Restricted tooltip text to 64 characters.
  40. //
  41. // Updated: 9 Nov 1999  - Now works in WindowsCE.
  42. //                        Fix for use in NT services (Thomas Mooney, TeleProc, Inc)
  43. //                        Added W2K stuff by Michael Dunn
  44. //
  45. // Updated: 1 Jan 2000  - Added tray minimisation stuff.
  46. // 
  47. // Updated: 21 Sep 2000 - Added GetDoWndAnimation - animation only occurs if the system
  48. //                        settings allow it (Matthew Ellis). Updated the GetTrayWndRect
  49. //                        function to include more fallback logic (Matthew Ellis)
  50. //                        NOTE: Signature of GetTrayWndRect has changed!
  51. //
  52. // This code may be used in compiled form in any way you desire. This
  53. // file may be redistributed unmodified by any means PROVIDING it is 
  54. // not sold for profit without the authors written consent, and 
  55. // providing that this notice and the authors name is included. If 
  56. // the source code in  this file is used in any commercial application 
  57. // then acknowledgement must be made to the author of this file 
  58. // (in whatever form you wish).
  59. //
  60. // This file is provided "as is" with no expressed or implied warranty.
  61. // The author accepts no liability for any damage caused through use.
  62. //
  63. // Expect bugs.
  64. // 
  65. // Please use and enjoy. Please let me know of any bugs/mods/improvements 
  66. // that you have found/implemented and I will fix/incorporate them into this
  67. // file. 
  68. //
  69. /////////////////////////////////////////////////////////////////////////////
  70.     
  71. #include "stdafx.h"
  72. #include "SystemTray.h"
  73.  
  74. #if defined(_DEBUG) && !defined(MMGR)
  75. #define new DEBUG_NEW
  76. #undef THIS_FILE
  77. static char THIS_FILE[] = __FILE__;
  78. #endif
  79.  
  80. #ifndef _WIN32_WCE  // Use C++ exception handling instead of structured.
  81. #undef TRY
  82. #undef CATCH
  83. #undef END_CATCH
  84. #define TRY try
  85. #define CATCH(ex_class, ex_object) catch(ex_class* ex_object)
  86. #define END_CATCH
  87. #endif  // _WIN32_WCE
  88.  
  89. #ifndef _countof
  90. #define _countof(x) ( sizeof(x) / sizeof(x[0]) )
  91. #endif
  92.  
  93. IMPLEMENT_DYNAMIC(CSystemTray, CWnd)
  94.  
  95. const UINT CSystemTray::m_nTimerID    = 4567;
  96. UINT CSystemTray::m_nMaxTooltipLength  = 64;     // This may change...
  97. const UINT CSystemTray::m_nTaskbarCreatedMsg = ::RegisterWindowMessage(_T("TaskbarCreated"));
  98. CWnd  CSystemTray::m_wndInvisible;
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CSystemTray construction/creation/destruction
  102.  
  103. CSystemTray::CSystemTray()
  104. {
  105.     Initialise();
  106. }
  107.  
  108. CSystemTray::CSystemTray(CWnd* pParent,             // The window that will recieve tray notifications
  109.                          UINT uCallbackMessage,     // the callback message to send to parent
  110.                          LPCTSTR szToolTip,         // tray icon tooltip
  111.                          HICON icon,                // Handle to icon
  112.                          UINT uID,                  // Identifier of tray icon
  113.                          BOOL bHidden /*=FALSE*/,   // Hidden on creation?                  
  114.                          LPCTSTR szBalloonTip /*=NULL*/,    // Ballon tip (w2k only)
  115.                          LPCTSTR szBalloonTitle /*=NULL*/,  // Balloon tip title (w2k)
  116.                          DWORD dwBalloonIcon /*=NIIF_NONE*/,// Ballon tip icon (w2k)
  117.                          UINT uBalloonTimeout /*=10*/)      // Balloon timeout (w2k)
  118. {
  119.     Initialise();
  120.     Create(pParent, uCallbackMessage, szToolTip, icon, uID, bHidden,
  121.            szBalloonTip, szBalloonTitle, dwBalloonIcon, uBalloonTimeout);
  122. }
  123.  
  124. void CSystemTray::Initialise()
  125. {
  126.     memset(&m_tnd, 0, sizeof(m_tnd));
  127.  
  128.     m_bEnabled = FALSE;
  129.     m_bHidden  = TRUE;
  130.     m_bRemoved = TRUE;
  131.  
  132.     m_DefaultMenuItemID    = 0;
  133.     m_DefaultMenuItemByPos = TRUE;
  134.  
  135.     m_bShowIconPending = FALSE;
  136.  
  137.     m_uIDTimer   = 0;
  138.     m_hSavedIcon = NULL;
  139.  
  140.     m_pTargetWnd = NULL;
  141.     m_uCreationFlags = 0;
  142.  
  143.     m_tnd.hIcon = NULL;
  144.  
  145.     m_nCurrentIcon = 0;
  146.  
  147. #ifdef SYSTEMTRAY_USEW2K
  148.     OSVERSIONINFO os = { sizeof(os) };
  149.     GetVersionEx(&os);
  150.     m_bWin2K = ( VER_PLATFORM_WIN32_NT == os.dwPlatformId && os.dwMajorVersion >= 5 );
  151. #else
  152.     m_bWin2K = FALSE;
  153. #endif
  154. }
  155.  
  156. // update by Michael Dunn, November 1999
  157. //
  158. //  New version of Create() that handles new features in Win 2K.
  159. //
  160. // Changes:
  161. //  szTip: Same as old, but can be 128 characters instead of 64.
  162. //  szBalloonTip: Text for a balloon tooltip that is shown when the icon
  163. //                is first added to the tray.  Pass "" if you don't want
  164. //                a balloon.
  165. //  szBalloonTitle: Title text for the balloon tooltip.  This text is shown
  166. //                  in bold above the szBalloonTip text.  Pass "" if you
  167. //                  don't want a title.
  168. //  dwBalloonIcon: Specifies which icon will appear in the balloon.  Legal
  169. //                 values are:
  170. //                     NIIF_NONE: No icon
  171. //                     NIIF_INFO: Information
  172. //                     NIIF_WARNING: Exclamation
  173. //                     NIIF_ERROR: Critical error (red circle with X)
  174. //  uBalloonTimeout: Number of seconds for the balloon to remain visible.
  175. //                   Must be between 10 and 30 inclusive.
  176.  
  177. BOOL CSystemTray::Create(CWnd* pParent, UINT uCallbackMessage, LPCTSTR szToolTip, 
  178.                          HICON icon, UINT uID, BOOL bHidden /*=FALSE*/,
  179.                          LPCTSTR szBalloonTip /*=NULL*/, 
  180.                          LPCTSTR szBalloonTitle /*=NULL*/,  
  181.                          DWORD dwBalloonIcon /*=NIIF_NONE*/,
  182.                          UINT uBalloonTimeout /*=10*/)
  183. {
  184. #ifdef _WIN32_WCE
  185.     m_bEnabled = TRUE;
  186. #else
  187.     // this is only for Windows 95 (or higher)
  188.     m_bEnabled = (GetVersion() & 0xff) >= 4;
  189.     if (!m_bEnabled) 
  190.     {
  191.         ASSERT(FALSE);
  192.         return FALSE;
  193.     }
  194. #endif
  195.  
  196.     m_nMaxTooltipLength = _countof(m_tnd.szTip);
  197.     
  198.     // Make sure we avoid conflict with other messages
  199.     ASSERT(uCallbackMessage >= WM_APP);
  200.  
  201.     // Tray only supports tooltip text up to m_nMaxTooltipLength) characters
  202.     ASSERT(AfxIsValidString(szToolTip));
  203.     ASSERT(_tcslen(szToolTip) <= m_nMaxTooltipLength);
  204.  
  205.     // Create an invisible window
  206.     CWnd::CreateEx(0, AfxRegisterWndClass(0), _T(""), WS_POPUP, 0,0,0,0, NULL, 0);
  207.  
  208.     // load up the NOTIFYICONDATA structure
  209.     m_tnd.cbSize = sizeof(NOTIFYICONDATA);
  210.     m_tnd.hWnd   = pParent->GetSafeHwnd()? pParent->GetSafeHwnd() : m_hWnd;
  211.     m_tnd.uID    = uID;
  212.     m_tnd.hIcon  = icon;
  213.     m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  214.     m_tnd.uCallbackMessage = uCallbackMessage;
  215.     _tcsncpy(m_tnd.szTip, szToolTip, m_nMaxTooltipLength-1);
  216.  
  217. #ifdef SYSTEMTRAY_USEW2K
  218.     if (m_bWin2K && szBalloonTip)
  219.     {
  220.         // The balloon tooltip text can be up to 255 chars long.
  221.         ASSERT(AfxIsValidString(szBalloonTip));
  222.         ASSERT(lstrlen(szBalloonTip) < 256);
  223.  
  224.         // The balloon title text can be up to 63 chars long.
  225.         if (szBalloonTitle)
  226.         {
  227.             ASSERT(AfxIsValidString(szBalloonTitle));
  228.             ASSERT(lstrlen(szBalloonTitle) < 64);
  229.         }
  230.  
  231.         // dwBalloonIcon must be valid.
  232.         ASSERT(NIIF_NONE == dwBalloonIcon    || NIIF_INFO == dwBalloonIcon ||
  233.                NIIF_WARNING == dwBalloonIcon || NIIF_ERROR == dwBalloonIcon);
  234.  
  235.         // The timeout must be between 10 and 30 seconds.
  236.         ASSERT(uBalloonTimeout >= 10 && uBalloonTimeout <= 30);
  237.  
  238.         m_tnd.uFlags |= NIF_INFO;
  239.  
  240.         _tcsncpy(m_tnd.szInfo, szBalloonTip, 255);
  241.         if (szBalloonTitle)
  242.             _tcsncpy(m_tnd.szInfoTitle, szBalloonTitle, 63);
  243.         else
  244.             m_tnd.szInfoTitle[0] = _T('\0');
  245.         m_tnd.uTimeout    = uBalloonTimeout * 1000; // convert time to ms
  246.         m_tnd.dwInfoFlags = dwBalloonIcon;
  247.     }
  248. #endif
  249.  
  250.     m_bHidden = bHidden;
  251.  
  252. #ifdef SYSTEMTRAY_USEW2K    
  253.     if (m_bWin2K && m_bHidden)
  254.     {
  255.         m_tnd.uFlags = NIF_STATE;
  256.         m_tnd.dwState = NIS_HIDDEN;
  257.         m_tnd.dwStateMask = NIS_HIDDEN;
  258.     }
  259. #endif
  260.  
  261.     m_uCreationFlags = m_tnd.uFlags;    // Store in case we need to recreate in OnTaskBarCreate
  262.  
  263.     BOOL bResult = TRUE;
  264.     if (!m_bHidden || m_bWin2K)
  265.     {
  266.         bResult = Shell_NotifyIcon(NIM_ADD, &m_tnd);
  267.         m_bShowIconPending = m_bHidden = m_bRemoved = !bResult;
  268.     }
  269.     
  270. #ifdef SYSTEMTRAY_USEW2K    
  271.     if (m_bWin2K && szBalloonTip)
  272.     {
  273.         // Zero out the balloon text string so that later operations won't redisplay
  274.         // the balloon.
  275.         m_tnd.szInfo[0] = _T('\0');
  276.     }
  277. #endif
  278.  
  279.     return bResult;
  280. }
  281.  
  282. CSystemTray::~CSystemTray()
  283. {
  284.     RemoveIcon();
  285.     m_IconList.clear();
  286.     DestroyWindow();
  287. }
  288.  
  289. /////////////////////////////////////////////////////////////////////////////
  290. // CSystemTray icon manipulation
  291.  
  292. //////////////////////////////////////////////////////////////////////////
  293. //
  294. // Function:    SetFocus()
  295. //
  296. // Description:
  297. //  Sets the focus to the tray icon.  Microsoft's Win 2K UI guidelines
  298. //  say you should do this after the user dismisses the icon's context
  299. //  menu.
  300. //
  301. // Input:
  302. //  Nothing.
  303. //
  304. // Returns:
  305. //  Nothing.
  306. //
  307. //////////////////////////////////////////////////////////////////////////
  308. // Added by Michael Dunn, November, 1999
  309. //////////////////////////////////////////////////////////////////////////
  310.  
  311. void CSystemTray::SetFocus()
  312. {
  313. #ifdef SYSTEMTRAY_USEW2K
  314.     Shell_NotifyIcon ( NIM_SETFOCUS, &m_tnd );
  315. #endif
  316. }
  317.  
  318. BOOL CSystemTray::MoveToRight()
  319. {
  320.     RemoveIcon();
  321.     return AddIcon();
  322. }
  323.  
  324. BOOL CSystemTray::AddIcon()
  325. {
  326.     if (!m_bRemoved)
  327.         RemoveIcon();
  328.  
  329.     if (m_bEnabled)
  330.     {
  331.         m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  332.         if (!Shell_NotifyIcon(NIM_ADD, &m_tnd))
  333.             m_bShowIconPending = TRUE;
  334.         else
  335.             m_bRemoved = m_bHidden = FALSE;
  336.     }
  337.     return (m_bRemoved == FALSE);
  338. }
  339.  
  340. BOOL CSystemTray::RemoveIcon()
  341. {
  342.     m_bShowIconPending = FALSE;
  343.  
  344.     if (!m_bEnabled || m_bRemoved)
  345.         return TRUE;
  346.  
  347.     m_tnd.uFlags = 0;
  348.     if (Shell_NotifyIcon(NIM_DELETE, &m_tnd))
  349.         m_bRemoved = m_bHidden = TRUE;
  350.  
  351.     return (m_bRemoved == TRUE);
  352. }
  353.  
  354. BOOL CSystemTray::HideIcon()
  355. {
  356.     if (!m_bEnabled || m_bRemoved || m_bHidden)
  357.         return TRUE;
  358.  
  359. #ifdef SYSTEMTRAY_USEW2K
  360.     if (m_bWin2K)
  361.     {
  362.         m_tnd.uFlags = NIF_STATE;
  363.         m_tnd.dwState = NIS_HIDDEN;
  364.         m_tnd.dwStateMask = NIS_HIDDEN;
  365.  
  366.         m_bHidden = Shell_NotifyIcon( NIM_MODIFY, &m_tnd);
  367.     }
  368.     else
  369. #endif
  370.         RemoveIcon();
  371.  
  372.     return (m_bHidden == TRUE);
  373. }
  374.  
  375. BOOL CSystemTray::ShowIcon()
  376. {
  377.     if (m_bRemoved)
  378.         return AddIcon();
  379.  
  380.     if (!m_bHidden)
  381.         return TRUE;
  382.  
  383. #ifdef SYSTEMTRAY_USEW2K
  384.     if (m_bWin2K)
  385.     {
  386.         m_tnd.uFlags = NIF_STATE;
  387.         m_tnd.dwState = 0;
  388.         m_tnd.dwStateMask = NIS_HIDDEN;
  389.         Shell_NotifyIcon ( NIM_MODIFY, &m_tnd );
  390.     }
  391.     else
  392. #endif
  393.         AddIcon();
  394.  
  395.     return (m_bHidden == FALSE);
  396. }
  397.  
  398. BOOL CSystemTray::SetIcon(HICON hIcon)
  399. {
  400.     if (!m_bEnabled)
  401.         return FALSE;
  402.  
  403.     HICON hOldIcon=m_tnd.hIcon;
  404.     m_tnd.uFlags = NIF_ICON;
  405.     m_tnd.hIcon = hIcon;
  406.  
  407.     BOOL res;
  408.     if (m_bHidden)
  409.         res=TRUE;
  410.     else
  411.         res=Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
  412.     if (hOldIcon)
  413.         DestroyIcon(hOldIcon);
  414.     return res;
  415. }
  416.  
  417. BOOL CSystemTray::SetIcon(LPCTSTR lpszIconName)
  418. {
  419.     HICON hIcon = (HICON) ::LoadImage(AfxGetResourceHandle(), 
  420.                                       lpszIconName,
  421.                                       IMAGE_ICON, 
  422.                                       0, 0,
  423.                                       LR_DEFAULTCOLOR);
  424.  
  425.     return SetIcon(hIcon);
  426. }
  427.  
  428. BOOL CSystemTray::SetIcon(UINT nIDResource)
  429. {
  430.     return SetIcon(MAKEINTRESOURCE(nIDResource));
  431. }
  432.  
  433. BOOL CSystemTray::SetStandardIcon(LPCTSTR lpIconName)
  434. {
  435.     HICON hIcon = LoadIcon(NULL, lpIconName);
  436.  
  437.     return SetIcon(hIcon);
  438. }
  439.  
  440. BOOL CSystemTray::SetStandardIcon(UINT nIDResource)
  441. {
  442.     return SetStandardIcon(MAKEINTRESOURCE(nIDResource));
  443. }
  444.  
  445. HICON CSystemTray::GetIcon() const
  446. {
  447.     return (m_bEnabled)? m_tnd.hIcon : NULL;
  448. }
  449.  
  450. BOOL CSystemTray::SetIconList(UINT uFirstIconID, UINT uLastIconID) 
  451. {
  452.     if (uFirstIconID > uLastIconID)
  453.         return FALSE;
  454.  
  455.     const CWinApp* pApp = AfxGetApp();
  456.     if (!pApp)
  457.     {
  458.         ASSERT(FALSE);
  459.         return FALSE;
  460.     }
  461.  
  462.     m_IconList.clear();
  463.     TRY {
  464.         for (UINT i = uFirstIconID; i <= uLastIconID; i++)
  465.             m_IconList.push_back(pApp->LoadIcon(i));
  466.     }
  467.     CATCH(CMemoryException, e)
  468.     {
  469.         e->ReportError();
  470.         e->Delete();
  471.         m_IconList.clear();
  472.         return FALSE;
  473.     }
  474.     END_CATCH
  475.  
  476.     return TRUE;
  477. }
  478.  
  479. BOOL CSystemTray::SetIconList(HICON* pHIconList, UINT nNumIcons)
  480. {
  481.     m_IconList.clear();
  482.  
  483.     TRY {
  484.         for (UINT i = 0; i <= nNumIcons; i++)
  485.             m_IconList.push_back(pHIconList[i]);
  486.     }
  487.     CATCH (CMemoryException, e)
  488.     {
  489.         e->ReportError();
  490.         e->Delete();
  491.         m_IconList.clear();
  492.         return FALSE;
  493.     }
  494.     END_CATCH
  495.  
  496.     return TRUE;
  497. }
  498.  
  499. BOOL CSystemTray::Animate(UINT nDelayMilliSeconds, int nNumSeconds /*=-1*/)
  500. {
  501.     StopAnimation();
  502.  
  503.     m_nCurrentIcon = 0;
  504.     m_StartTime = COleDateTime::GetCurrentTime();
  505.     m_nAnimationPeriod = nNumSeconds;
  506.     m_hSavedIcon = GetIcon();
  507.  
  508.     // Setup a timer for the animation
  509.     m_uIDTimer = SetTimer(m_nTimerID, nDelayMilliSeconds, NULL);
  510.  
  511.     return (m_uIDTimer != 0);
  512. }
  513.  
  514. BOOL CSystemTray::StepAnimation()
  515. {
  516.     if (m_IconList.empty())
  517.         return FALSE;
  518.  
  519.     m_nCurrentIcon++;
  520.     if (m_nCurrentIcon >= m_IconList.size())
  521.         m_nCurrentIcon = 0;
  522.  
  523.     return SetIcon(m_IconList[m_nCurrentIcon]);
  524. }
  525.  
  526. BOOL CSystemTray::StopAnimation()
  527. {
  528.     BOOL bResult = FALSE;
  529.  
  530.     if (m_uIDTimer)
  531.         bResult = KillTimer(m_uIDTimer);
  532.     m_uIDTimer = 0;
  533.  
  534.     if (m_hSavedIcon)
  535.         SetIcon(m_hSavedIcon);
  536.     m_hSavedIcon = NULL;
  537.  
  538.     return bResult;
  539. }
  540.  
  541. /////////////////////////////////////////////////////////////////////////////
  542. // CSystemTray tooltip text manipulation
  543.  
  544. BOOL CSystemTray::SetTooltipText(LPCTSTR pszTip)
  545. {
  546.     ASSERT(AfxIsValidString(pszTip)); // (md)
  547.     ASSERT(_tcslen(pszTip) < m_nMaxTooltipLength);
  548.  
  549.     if (!m_bEnabled) 
  550.         return FALSE;
  551.  
  552.     m_tnd.uFlags = NIF_TIP;
  553.     _tcsncpy(m_tnd.szTip, pszTip, m_nMaxTooltipLength-1);
  554.  
  555.     if (m_bHidden)
  556.         return TRUE;
  557.     else
  558.         return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
  559. }
  560.  
  561. BOOL CSystemTray::SetTooltipText(UINT nID)
  562. {
  563.     CString strText;
  564.     VERIFY(strText.LoadString(nID));
  565.  
  566.     return SetTooltipText(strText);
  567. }
  568.  
  569. CString CSystemTray::GetTooltipText() const
  570. {
  571.     CString strText;
  572.     if (m_bEnabled)
  573.         strText = m_tnd.szTip;
  574.  
  575.     return strText;
  576. }
  577.  
  578. /////////////////////////////////////////////////////////////////////////////
  579. // CSystemTray support for Win 2K features.
  580.  
  581. //////////////////////////////////////////////////////////////////////////
  582. //
  583. // Function:    ShowBalloon
  584. //
  585. // Description:
  586. //  Shows a balloon tooltip over the tray icon.
  587. //
  588. // Input:
  589. //  szText: [in] Text for the balloon tooltip.
  590. //  szTitle: [in] Title for the balloon.  This text is shown in bold above
  591. //           the tooltip text (szText).  Pass "" if you don't want a title.
  592. //  dwIcon: [in] Specifies an icon to appear in the balloon.  Legal values are:
  593. //                 NIIF_NONE: No icon
  594. //                 NIIF_INFO: Information
  595. //                 NIIF_WARNING: Exclamation
  596. //                 NIIF_ERROR: Critical error (red circle with X)
  597. //  uTimeout: [in] Number of seconds for the balloon to remain visible.  Can
  598. //            be between 10 and 30 inclusive.
  599. //
  600. // Returns:
  601. //  TRUE if successful, FALSE if not.
  602. //
  603. //////////////////////////////////////////////////////////////////////////
  604. // Added by Michael Dunn, November 1999
  605. //////////////////////////////////////////////////////////////////////////
  606.  
  607. BOOL CSystemTray::ShowBalloon(LPCTSTR szText,
  608.                               LPCTSTR szTitle  /*=NULL*/,
  609.                               DWORD   dwIcon   /*=NIIF_NONE*/,
  610.                               UINT    uTimeout /*=10*/ )
  611. {
  612. #ifndef SYSTEMTRAY_USEW2K
  613.     return FALSE;
  614. #else
  615.     // Bail out if we're not on Win 2K.
  616.     if (!m_bWin2K)
  617.         return FALSE;
  618.  
  619.     // Verify input parameters.
  620.  
  621.     // The balloon tooltip text can be up to 255 chars long.
  622.     ASSERT(AfxIsValidString(szText));
  623.     ASSERT(lstrlen(szText) < 256);
  624.  
  625.     // The balloon title text can be up to 63 chars long.
  626.     if (szTitle)
  627.     {
  628.         ASSERT(AfxIsValidString( szTitle));
  629.         ASSERT(lstrlen(szTitle) < 64);
  630.     }
  631.  
  632.     // dwBalloonIcon must be valid.
  633.     ASSERT(NIIF_NONE == dwIcon    || NIIF_INFO == dwIcon ||
  634.            NIIF_WARNING == dwIcon || NIIF_ERROR == dwIcon);
  635.  
  636.     // The timeout must be between 10 and 30 seconds.
  637.     ASSERT(uTimeout >= 10 && uTimeout <= 30);
  638.  
  639.  
  640.     m_tnd.uFlags = NIF_INFO;
  641.     _tcsncpy(m_tnd.szInfo, szText, 256);
  642.     if (szTitle)
  643.         _tcsncpy(m_tnd.szInfoTitle, szTitle, 64);
  644.     else
  645.         m_tnd.szInfoTitle[0] = _T('\0');
  646.     m_tnd.dwInfoFlags = dwIcon;
  647.     m_tnd.uTimeout = uTimeout * 1000;   // convert time to ms
  648.  
  649.     BOOL bSuccess = Shell_NotifyIcon (NIM_MODIFY, &m_tnd);
  650.  
  651.     // Zero out the balloon text string so that later operations won't redisplay
  652.     // the balloon.
  653.     m_tnd.szInfo[0] = _T('\0');
  654.  
  655.     return bSuccess;
  656. #endif
  657. }
  658.  
  659. /////////////////////////////////////////////////////////////////////////////
  660. // CSystemTray notification window stuff
  661.  
  662. BOOL CSystemTray::SetNotificationWnd(CWnd* pWnd)
  663. {
  664.     if (!m_bEnabled) 
  665.         return FALSE;
  666.  
  667.     // Make sure Notification window is valid
  668.     if (!pWnd || !::IsWindow(pWnd->GetSafeHwnd()))
  669.     {
  670.         ASSERT(FALSE);
  671.         return FALSE;
  672.     }
  673.  
  674.     m_tnd.hWnd = pWnd->GetSafeHwnd();
  675.     m_tnd.uFlags = 0;
  676.  
  677.     if (m_bHidden)
  678.         return TRUE;
  679.     else
  680.         return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
  681. }
  682.  
  683. CWnd* CSystemTray::GetNotificationWnd() const
  684. {
  685.     return CWnd::FromHandle(m_tnd.hWnd);
  686. }
  687.  
  688. // Hatr added
  689.  
  690. // Hatr added
  691.  
  692. // Change or retrive the window to send menu commands to
  693. BOOL CSystemTray::SetTargetWnd(CWnd* pTargetWnd)
  694. {
  695.     m_pTargetWnd = pTargetWnd;
  696.     return TRUE;
  697. } // CSystemTray::SetTargetWnd()
  698.  
  699. CWnd* CSystemTray::GetTargetWnd() const
  700. {
  701.     if (m_pTargetWnd)
  702.         return m_pTargetWnd;
  703.     else
  704.         return AfxGetMainWnd();
  705. } // CSystemTray::GetTargetWnd()
  706.  
  707. /////////////////////////////////////////////////////////////////////////////
  708. // CSystemTray notification message stuff
  709.  
  710. BOOL CSystemTray::SetCallbackMessage(UINT uCallbackMessage)
  711. {
  712.     if (!m_bEnabled)
  713.         return FALSE;
  714.  
  715.     // Make sure we avoid conflict with other messages
  716.     ASSERT(uCallbackMessage >= WM_APP);
  717.  
  718.     m_tnd.uCallbackMessage = uCallbackMessage;
  719.     m_tnd.uFlags = NIF_MESSAGE;
  720.  
  721.     if (m_bHidden)
  722.         return TRUE;
  723.     else
  724.         return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
  725. }
  726.  
  727. UINT CSystemTray::GetCallbackMessage() const
  728. {
  729.     return m_tnd.uCallbackMessage;
  730. }
  731.  
  732. /////////////////////////////////////////////////////////////////////////////
  733. // CSystemTray menu manipulation
  734.  
  735. BOOL CSystemTray::SetMenuDefaultItem(UINT uItem, BOOL bByPos)
  736. {
  737. #ifdef _WIN32_WCE
  738.     return FALSE;
  739. #else
  740.     if ((m_DefaultMenuItemID == uItem) && (m_DefaultMenuItemByPos == bByPos)) 
  741.         return TRUE;
  742.  
  743.     m_DefaultMenuItemID = uItem;
  744.     m_DefaultMenuItemByPos = bByPos;   
  745.  
  746.     CMenu menu, *pSubMenu;
  747.  
  748.     if (!menu.LoadMenu(m_tnd.uID))
  749.         return FALSE;
  750.  
  751.     pSubMenu = menu.GetSubMenu(0);
  752.     if (!pSubMenu)
  753.         return FALSE;
  754.  
  755.     ::SetMenuDefaultItem(pSubMenu->m_hMenu, m_DefaultMenuItemID, m_DefaultMenuItemByPos);
  756.  
  757.     return TRUE;
  758. #endif
  759. }
  760.  
  761. void CSystemTray::GetMenuDefaultItem(UINT& uItem, BOOL& bByPos)
  762. {
  763.     uItem = m_DefaultMenuItemID;
  764.     bByPos = m_DefaultMenuItemByPos;
  765. }
  766.  
  767. /////////////////////////////////////////////////////////////////////////////
  768. // CSystemTray message handlers
  769.  
  770. BEGIN_MESSAGE_MAP(CSystemTray, CWnd)
  771.     //{{AFX_MSG_MAP(CSystemTray)
  772.     ON_WM_TIMER()
  773.     //}}AFX_MSG_MAP
  774. #ifndef _WIN32_WCE
  775.     ON_WM_SETTINGCHANGE()
  776. #endif
  777.     ON_REGISTERED_MESSAGE(CSystemTray::m_nTaskbarCreatedMsg, OnTaskbarCreated)
  778. END_MESSAGE_MAP()
  779.  
  780. void CSystemTray::OnTimer(UINT_PTR nIDEvent) 
  781. {
  782.     if (nIDEvent != m_uIDTimer)
  783.     {
  784.         ASSERT(FALSE);
  785.         return;
  786.     }
  787.  
  788.     COleDateTime CurrentTime = COleDateTime::GetCurrentTime();
  789.     COleDateTimeSpan period = CurrentTime - m_StartTime;
  790.  
  791.     if (m_nAnimationPeriod > 0 && m_nAnimationPeriod < period.GetTotalSeconds())
  792.     {
  793.         StopAnimation();
  794.         return;
  795.     }
  796.  
  797.     StepAnimation();
  798. }
  799.  
  800. // This is called whenever the taskbar is created (eg after explorer crashes
  801. // and restarts. Please note that the WM_TASKBARCREATED message is only passed
  802. // to TOP LEVEL windows (like WM_QUERYNEWPALETTE)
  803. LRESULT CSystemTray::OnTaskbarCreated(WPARAM /*wParam*/, LPARAM /*lParam*/) 
  804. {
  805.     InstallIconPending();
  806.     return 0;
  807. }
  808.  
  809. #ifndef _WIN32_WCE
  810. void CSystemTray::OnSettingChange(UINT uFlags, LPCTSTR lpszSection) 
  811. {
  812.     CWnd::OnSettingChange(uFlags, lpszSection);
  813.  
  814.     if (uFlags == SPI_SETWORKAREA)
  815.         InstallIconPending();    
  816. }
  817. #endif
  818.  
  819. LRESULT CSystemTray::OnTrayNotification(WPARAM wParam, LPARAM lParam) 
  820. {
  821.     //Return quickly if its not for this tray icon
  822.     if (wParam != m_tnd.uID)
  823.         return 0L;
  824.  
  825.     CMenu menu, *pSubMenu;
  826.     CWnd *pTargetWnd = GetTargetWnd();
  827.     if (!pTargetWnd)
  828.         return 0L;
  829.  
  830.     // Clicking with right button brings up a context menu
  831. #if defined(_WIN32_WCE) //&& _WIN32_WCE < 211
  832.     BOOL bAltPressed = ((GetKeyState(VK_MENU) & (1 << (sizeof(SHORT)*8-1))) != 0);
  833.     if (LOWORD(lParam) == WM_LBUTTONUP && bAltPressed)
  834. #else
  835.     if (LOWORD(lParam) == WM_RBUTTONUP)
  836. #endif
  837.     {    
  838.         if (!menu.LoadMenu(m_tnd.uID))
  839.             return 0;
  840.         
  841.         pSubMenu = menu.GetSubMenu(0);
  842.         if (!pSubMenu)
  843.             return 0;
  844.  
  845. #ifndef _WIN32_WCE
  846.         // Make chosen menu item the default (bold font)
  847.         ::SetMenuDefaultItem(pSubMenu->m_hMenu, m_DefaultMenuItemID, m_DefaultMenuItemByPos);
  848. #endif
  849.  
  850.         // Display and track the popup menu
  851.         CPoint pos;
  852. #ifdef _WIN32_WCE
  853.         pos = CPoint(GetMessagePos());
  854. #else
  855.         GetCursorPos(&pos);
  856. #endif
  857.  
  858.         pTargetWnd->SetForegroundWindow(); 
  859.         
  860. #ifndef _WIN32_WCE
  861.         ::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, 
  862.                          pTargetWnd->GetSafeHwnd(), NULL);
  863. #else
  864.         pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, pos.x, pos.y, pTargetWnd, NULL);
  865. #endif
  866.  
  867.         // BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly"
  868.         pTargetWnd->PostMessage(WM_NULL, 0, 0);
  869.  
  870.         menu.DestroyMenu();
  871.     } 
  872. #if defined(_WIN32_WCE) //&& _WIN32_WCE < 211
  873.     if (LOWORD(lParam) == WM_LBUTTONDBLCLK && bAltPressed)
  874. #else
  875.     else if (LOWORD(lParam) == WM_LBUTTONDBLCLK) 
  876. #endif
  877.     {
  878.         // double click received, the default action is to execute default menu item
  879.         pTargetWnd->SetForegroundWindow();  
  880.  
  881.         UINT uItem;
  882.         if (m_DefaultMenuItemByPos)
  883.         {
  884.             if (!menu.LoadMenu(m_tnd.uID))
  885.                 return 0;
  886.             
  887.             pSubMenu = menu.GetSubMenu(0);
  888.             if (!pSubMenu)
  889.                 return 0;
  890.             
  891.             uItem = pSubMenu->GetMenuItemID(m_DefaultMenuItemID);
  892.  
  893.             menu.DestroyMenu();
  894.         }
  895.         else
  896.             uItem = m_DefaultMenuItemID;
  897.         
  898.         pTargetWnd->SendMessage(WM_COMMAND, uItem, 0);
  899.     }
  900.  
  901.     return 1;
  902. }
  903.  
  904. LRESULT CSystemTray::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  905. {
  906.     if (message == m_tnd.uCallbackMessage)
  907.         return OnTrayNotification(wParam, lParam);
  908.     
  909.     return CWnd::WindowProc(message, wParam, lParam);
  910. }
  911.  
  912. void CSystemTray::InstallIconPending()
  913. {
  914.     // Is the icon display pending, and it's not been set as "hidden"?
  915.     if (!m_bShowIconPending || m_bHidden)
  916.         return;
  917.  
  918.     // Reset the flags to what was used at creation
  919.     m_tnd.uFlags = m_uCreationFlags;
  920.  
  921.     // Try and recreate the icon
  922.     m_bHidden = !Shell_NotifyIcon(NIM_ADD, &m_tnd);
  923.  
  924.     // If it's STILL hidden, then have another go next time...
  925.     m_bShowIconPending = !m_bHidden;
  926.  
  927.     ASSERT(m_bHidden == FALSE);
  928. }
  929.  
  930. /////////////////////////////////////////////////////////////////////////////
  931. // For minimising/maximising from system tray
  932.  
  933. static BOOL CALLBACK FindTrayWnd(HWND hwnd, LPARAM lParam)
  934. {
  935.     TCHAR szClassName[256];
  936.     GetClassName(hwnd, szClassName, 255);
  937.  
  938.     // Did we find the Main System Tray? If so, then get its size and keep going
  939.     if (_tcscmp(szClassName, _T("TrayNotifyWnd")) == 0)
  940.     {
  941.         CRect *pRect = (CRect*) lParam;
  942.         ::GetWindowRect(hwnd, pRect);
  943.         return TRUE;
  944.     }
  945.  
  946.     // Did we find the System Clock? If so, then adjust the size of the rectangle
  947.     // we have and quit (clock will be found after the system tray)
  948.     if (_tcscmp(szClassName, _T("TrayClockWClass")) == 0)
  949.     {
  950.         CRect *pRect = (CRect*) lParam;
  951.         CRect rectClock;
  952.         ::GetWindowRect(hwnd, rectClock);
  953.         // if clock is above system tray adjust accordingly
  954.         if (rectClock.bottom < pRect->bottom-5) // 10 = random fudge factor.
  955.             pRect->top = rectClock.bottom;
  956.         else
  957.             pRect->right = rectClock.left;
  958.         return FALSE;
  959.     }
  960.  
  961.     return TRUE;
  962. }
  963.  
  964. #ifndef _WIN32_WCE
  965. // enhanced version by Matthew Ellis <m.t.ellis@bigfoot.com>
  966. void CSystemTray::GetTrayWndRect(LPRECT lprect)
  967. {
  968. #define DEFAULT_RECT_WIDTH 150
  969. #define DEFAULT_RECT_HEIGHT 30
  970.  
  971.     HWND hShellTrayWnd = ::FindWindow(_T("Shell_TrayWnd"), NULL);
  972.     if (hShellTrayWnd)
  973.     {
  974.         ::GetWindowRect(hShellTrayWnd, lprect);
  975.         EnumChildWindows(hShellTrayWnd, FindTrayWnd, (LPARAM)lprect);
  976.         return;
  977.     }
  978.     // OK, we failed to get the rect from the quick hack. Either explorer isn't
  979.     // running or it's a new version of the shell with the window class names
  980.     // changed (how dare Microsoft change these undocumented class names!) So, we
  981.     // try to find out what side of the screen the taskbar is connected to. We
  982.     // know that the system tray is either on the right or the bottom of the
  983.     // taskbar, so we can make a good guess at where to minimize to
  984.     APPBARDATA appBarData;
  985.     appBarData.cbSize=sizeof(appBarData);
  986.     if (SHAppBarMessage(ABM_GETTASKBARPOS,&appBarData))
  987.     {
  988.         // We know the edge the taskbar is connected to, so guess the rect of the
  989.         // system tray. Use various fudge factor to make it look good
  990.         switch(appBarData.uEdge)
  991.         {
  992.         case ABE_LEFT:
  993.         case ABE_RIGHT:
  994.             // We want to minimize to the bottom of the taskbar
  995.             lprect->top    = appBarData.rc.bottom-100;
  996.             lprect->bottom = appBarData.rc.bottom-16;
  997.             lprect->left   = appBarData.rc.left;
  998.             lprect->right  = appBarData.rc.right;
  999.             break;
  1000.             
  1001.         case ABE_TOP:
  1002.         case ABE_BOTTOM:
  1003.             // We want to minimize to the right of the taskbar
  1004.             lprect->top    = appBarData.rc.top;
  1005.             lprect->bottom = appBarData.rc.bottom;
  1006.             lprect->left   = appBarData.rc.right-100;
  1007.             lprect->right  = appBarData.rc.right-16;
  1008.             break;
  1009.         }
  1010.         return;
  1011.     }
  1012.     
  1013.     // Blimey, we really aren't in luck. It's possible that a third party shell
  1014.     // is running instead of explorer. This shell might provide support for the
  1015.     // system tray, by providing a Shell_TrayWnd window (which receives the
  1016.     // messages for the icons) So, look for a Shell_TrayWnd window and work out
  1017.     // the rect from that. Remember that explorer's taskbar is the Shell_TrayWnd,
  1018.     // and stretches either the width or the height of the screen. We can't rely
  1019.     // on the 3rd party shell's Shell_TrayWnd doing the same, in fact, we can't
  1020.     // rely on it being any size. The best we can do is just blindly use the
  1021.     // window rect, perhaps limiting the width and height to, say 150 square.
  1022.     // Note that if the 3rd party shell supports the same configuraion as
  1023.     // explorer (the icons hosted in NotifyTrayWnd, which is a child window of
  1024.     // Shell_TrayWnd), we would already have caught it above
  1025.     if (hShellTrayWnd)
  1026.     {
  1027.         ::GetWindowRect(hShellTrayWnd, lprect);
  1028.         if (lprect->right - lprect->left > DEFAULT_RECT_WIDTH)
  1029.             lprect->left = lprect->right - DEFAULT_RECT_WIDTH;
  1030.         if (lprect->bottom - lprect->top > DEFAULT_RECT_HEIGHT)
  1031.             lprect->top = lprect->bottom - DEFAULT_RECT_HEIGHT;
  1032.         
  1033.         return;
  1034.     }
  1035.     
  1036.     // OK. Haven't found a thing. Provide a default rect based on the current work
  1037.     // area
  1038.     SystemParametersInfo(SPI_GETWORKAREA,0, lprect, 0);
  1039.     lprect->left = lprect->right - DEFAULT_RECT_WIDTH;
  1040.     lprect->top  = lprect->bottom - DEFAULT_RECT_HEIGHT;
  1041. }
  1042.  
  1043. // Check to see if the animation has been disabled (Matthew Ellis <m.t.ellis@bigfoot.com>)
  1044. BOOL CSystemTray::GetDoWndAnimation()
  1045. {
  1046.   ANIMATIONINFO ai;
  1047.  
  1048.   ai.cbSize=sizeof(ai);
  1049.   SystemParametersInfo(SPI_GETANIMATION,sizeof(ai),&ai,0);
  1050.  
  1051.   return ai.iMinAnimate?TRUE:FALSE;
  1052. }
  1053. #endif
  1054.  
  1055. BOOL CSystemTray::RemoveTaskbarIcon(CWnd* pWnd)
  1056. {
  1057.     LPCTSTR pstrOwnerClass = AfxRegisterWndClass(0);
  1058.  
  1059.     // Create static invisible window
  1060.     if (!::IsWindow(m_wndInvisible.m_hWnd))
  1061.     {
  1062.         if (!m_wndInvisible.CreateEx(0, pstrOwnerClass, _T(""), WS_POPUP,
  1063.                 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  1064.                 NULL, 0))
  1065.             return FALSE;
  1066.     }
  1067.  
  1068.     pWnd->SetParent(&m_wndInvisible);
  1069.  
  1070.     return TRUE;
  1071. }
  1072.  
  1073. void CSystemTray::MinimiseToTray(CWnd* pWnd)
  1074. {
  1075. #ifndef _WIN32_WCE
  1076.     if (GetDoWndAnimation())
  1077.     {
  1078.         CRect rectFrom, rectTo;
  1079.  
  1080.         pWnd->GetWindowRect(rectFrom);
  1081.         GetTrayWndRect(rectTo);
  1082.  
  1083.         ::DrawAnimatedRects(pWnd->m_hWnd, IDANI_CAPTION, rectFrom, rectTo);
  1084.     }
  1085.  
  1086.     RemoveTaskbarIcon(pWnd);
  1087.     pWnd->ModifyStyle(WS_VISIBLE, 0);
  1088. #endif
  1089. }
  1090.  
  1091. void CSystemTray::MaximiseFromTray(CWnd* pWnd)
  1092. {
  1093. #ifndef _WIN32_WCE
  1094.     if (GetDoWndAnimation())
  1095.     {
  1096.         CRect rectTo;
  1097.         pWnd->GetWindowRect(rectTo);
  1098.  
  1099.         CRect rectFrom;
  1100.         GetTrayWndRect(rectFrom);
  1101.  
  1102.         pWnd->SetParent(NULL);
  1103.         ::DrawAnimatedRects(pWnd->m_hWnd, IDANI_CAPTION, rectFrom, rectTo);
  1104.     }
  1105.     else
  1106.         pWnd->SetParent(NULL);
  1107.  
  1108.     pWnd->ModifyStyle(0, WS_VISIBLE);
  1109.     pWnd->RedrawWindow(NULL, NULL, RDW_UPDATENOW | RDW_ALLCHILDREN | RDW_FRAME |
  1110.                                    RDW_INVALIDATE | RDW_ERASE);
  1111.  
  1112.     // Move focus away and back again to ensure taskbar icon is recreated
  1113.     if (::IsWindow(m_wndInvisible.m_hWnd))
  1114.         m_wndInvisible.SetActiveWindow();
  1115.     pWnd->SetActiveWindow();
  1116.     pWnd->SetForegroundWindow();
  1117. #endif
  1118. }
  1119.  
  1120. void CSystemTray::RefreshIcon()
  1121. {
  1122.     
  1123.     if (!m_bEnabled)
  1124.         return;
  1125.     m_tnd.uFlags = 0;
  1126.     if (!Shell_NotifyIcon(NIM_MODIFY, &m_tnd))
  1127.     {
  1128.         MoveToRight();
  1129.     }
  1130. }